﻿using System.Web.Mvc;
using Coderoom.ScrumPlanner.Data;
using Coderoom.ScrumPlanner.Domain;

namespace Coderoom.ScrumPlanner.Web.Controllers
{
	public sealed class ProjectController : CoderoomController
	{
		private readonly ProjectService _projectService;

		public ProjectController(ProjectService projectService)
		{
			_projectService = projectService;
		}

		public ProjectService ProjectService
		{
			get { return _projectService; }
		}

		#region Actions

		public ActionResult List()
		{
			return View(_projectService.GetProjects());
		}

		public ActionResult Detail(string slug)
		{
			var project = _projectService.GetProject(slug);

			if (project != null)
			{
				return View<ProjectController>(x => x.Detail(null));
			}

			
		}

		public ActionResult New()
		{
			return View<ProjectController>(controller => controller.New());
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateAntiForgeryToken(Salt = "0FC697D4-5AF3-4091-8361-66602440DE52")]
		public ActionResult New(string title, string slug)
		{
			var newProject = new Project {Title = title, Slug = slug};

			var operationResult = _projectService.CreateProject(newProject);

			if (operationResult.IsSuccessful)
				return RedirectToAction<ProjectController>(controller => controller.Detail(slug));
			else
			{
				foreach (var error in operationResult.ValidationErrors)
					ModelState.AddModelError(error.PropertyName, error.Message);

				return View<ProjectController>(controller => controller.New());
			}
		}

		[AcceptVerbs(HttpVerbs.Post)]
		[ValidateAntiForgeryToken(Salt = "0FC697D4-5AF3-4091-8361-66602440DE52")]
		public ActionResult Delete(string slug)
		{
			throw new System.NotImplementedException();
		}

		#endregion
	}
}
